package com.lianxi.hutool;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.swing.clipboard.ClipboardUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.*;
import cn.hutool.extra.ftp.Ftp;
import cn.hutool.extra.ftp.FtpMode;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.alibaba.fastjson.JSON;
import com.lianxi.model.News;
import org.junit.Test;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class MyHutoolTest {


    public static void main(String[] args) {

    }

    @Test
    public void test() {
        //获得随机数
        String s = RandomUtil.randomNumbers(5);
        System.out.println(s);
    }

    @Test
    public void test2() {
        ClipboardUtil.setStr("设置剪贴板");
        //身份证验证
        boolean validCard = IdcardUtil.isValidCard("41132819980325505X");
        System.out.println(validCard);

        // 构建node列表
        List<TreeNode<String>> nodeList = CollUtil.newArrayList();
        nodeList.add(new TreeNode<>("1", "0", "系统管理", 5));
        nodeList.add(new TreeNode<>("11", "1", "用户管理", 222222));
        nodeList.add(new TreeNode<>("111", "11", "用户添加", 0));
        nodeList.add(new TreeNode<>("2", "0", "店铺管理", 1));
        nodeList.add(new TreeNode<>("21", "2", "商品管理", 44));
        nodeList.add(new TreeNode<>("221", "2", "商品管理2", 2));
        String s = JSON.toJSONString(nodeList);
        System.out.println(s);
    }


    /**
     * 生成二维码
     */
    @Test
    public void test3() {
        QrCodeUtil.generate(//
                "http://hutool.cn/", //二维码内容
                QrConfig.create().setImg("d:/Desktop/微信图片_20220817144745.png"), //附带logo
                FileUtil.file("d:/Desktop/qrcodeWithLogo.jpg")//写出到的文件
        );
    }


    /**
     * HUTOOL ftp
     */
    @Test
    public void test4() throws IOException {
        Ftp ftp = new Ftp("10.10.10.249", 21, "t1file", "A8aIm1Gs4V0Fqb");
        boolean cd = ftp.cd("/1/");
        System.out.println(cd);
        ftp.upload("/1/", FileUtil.file("d:/Desktop/微信图片_20220817144745.png"));
        ftp.download("/1/", "微信图片_20220817144745.png", FileUtil.file("D:/Desktop/test2.jpg"));
        ftp.close();
    }


    /**
     * HUTOOL ftp
     */
    @Test
    public void test5() throws IOException {
        Ftp ftp = new Ftp("10.10.10.193");
        boolean cd = ftp.cd("/Desktop/images/");
        System.out.println(cd);
        ftp.upload("/Desktop/images/", FileUtil.file("d:/Desktop/素材/下载 - 副本 (6).jpg"));
        ftp.close();
    }


    @Test
    public void test6() {
        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        byte[] imageBytes = lineCaptcha.getImageBytes();
        String stringImg = "data:image/jpeg;base64," + Base64.encode(imageBytes);
        FileUtil.writeUtf8String(stringImg, "d:/Desktop/test.txt");
        String key = IdUtil.simpleUUID();
//        System.out.println(stringImg);
//        //图形验证码写出，可以写出到文件，也可以写出到流
//                lineCaptcha.write("d:/line.png");
//        //输出code
//                Console.log(lineCaptcha.getCode());
//        //验证图形验证码的有效性，返回boolean值
//                boolean verify = lineCaptcha.verify("1234");
//                System.out.println(verify);

    }


    @Test
    public void test7() {
        String s = IdUtil.simpleUUID();
        System.out.println(s);
        HashMap<String, String> map = MapUtil.newHashMap();
    }


    @Test
    public void test8() {
        String a = "2017-05-06 21:14:56";
        Date date = Convert.toDate(a);
        System.out.println(date);
        System.out.println(Convert.toStr(date));
    }


    //分组计时
    @Test
    public void test9() {
        final TimeInterval timer = new TimeInterval();

        // 分组1
        timer.start("1");
        ThreadUtil.sleep(800);

        // 分组2
        timer.start("2");
        ThreadUtil.sleep(900);
        timer.start("3");
        Console.log("Timer 1 took {} ms", timer.intervalMs("1"));
        Console.log("Timer 2 took {} ms", timer.intervalMs("2"));

        Console.log("Timer total took {} ms", timer.intervalMs());

    }

    //分组计时
    @Test
    public void test10() {
        StopWatch stopWatch = new StopWatch();
        // 分组1
        stopWatch.start();
        ThreadUtil.sleep(800);
        stopWatch.stop();
        // 分组2
        stopWatch.start();
        ThreadUtil.sleep(900);
        stopWatch.stop();
        Console.log(stopWatch.prettyPrint());
        Console.log(stopWatch.getTotalTimeSeconds());
    }


}
